package com.samphanie.ane.library.log;

import androidx.annotation.NonNull;

import com.samphanie.ane.library.util.AneStackTraceUtil;

import java.util.Arrays;
import java.util.List;

/**
 * @author ZSY
 * Tips :
 * 1. 打印堆栈信息
 * 2. File输出
 * 3. 模拟控制台
 */
public class AneLog {

    private static final String OASE_LOG_PACKAGE;

    static {
        String className = AneLog.class.getName();
        OASE_LOG_PACKAGE = className.substring(0, className.lastIndexOf('.') + 1);
    }

    public static void v(Object... contents) {
        log(AneLogType.V, contents);
    }

    public static void vt(String tag, Object... contents) {
        log(AneLogType.V, tag, contents);
    }

    public static void e(Object... contents) {
        log(AneLogType.E, contents);
    }

    public static void et(String tag, Object... contents) {
        log(AneLogType.E, tag, contents);
    }

    public static void a(Object... contents) {
        log(AneLogType.A, contents);
    }

    public static void at(String tag, Object... contents) {
        log(AneLogType.A, tag, contents);
    }

    public static void d(Object... contents) {
        log(AneLogType.D, contents);
    }

    public static void dt(String tag, Object... contents) {
        log(AneLogType.D, tag, contents);
    }

    public static void i(Object... contents) {
        log(AneLogType.I, contents);
    }

    public static void it(String tag, Object... contents) {
        log(AneLogType.I, tag, contents);
    }

    public static void w(Object... contents) {
        log(AneLogType.W, contents);
    }

    public static void wt(String tag, Object... contents) {
        log(AneLogType.W, tag, contents);
    }

    public static void log(@AneLogType.TYPE int type, Object... contents) {
        log(type, AneLogManager.getInstance().getConfig().getGlobalTag(), contents);
    }

    public static void log(@AneLogType.TYPE int type, @NonNull String tag, Object... contents) {
        log(AneLogManager.getInstance().getConfig(), type, tag, contents);
    }

    public static void log(@NonNull AneLogConfig config, @AneLogType.TYPE int type, @NonNull String tag, Object... contents) {

        if (!config.enable()) {
            return;
        }

        StringBuilder sb = new StringBuilder();
        if (config.includeThread()) {
            String threadInfo = AneLogConfig.HI_THREAD_FORMATTER.format(Thread.currentThread());
            sb.append(threadInfo).append("\n");
        }
        if (config.stackTraceDepth() > 0) {
            String stackTrace = AneLogConfig.HI_STACK_TRACE_FORMATTER.format(AneStackTraceUtil.getCroppedRealStackTrack(new Throwable().getStackTrace(), OASE_LOG_PACKAGE, config.stackTraceDepth()));
            sb.append(stackTrace).append("\n");
        }
        String body = parseBody(contents, config);
        sb.append(body);
        // Log.println(type, tag, body);
        List<AneLogPrinter> printers = config.printers() != null ? Arrays.asList(config.printers()) : AneLogManager.getInstance().getPrinters();
        if (printers == null) {
            return;
        }

        // 打印Log
        for (AneLogPrinter printer : printers) {
            printer.print(config, type, tag, sb.toString());
        }
    }

    private static String parseBody(@NonNull Object[] contents, @NonNull AneLogConfig config) {

        if (config.injectJsonParser() != null) {
            return config.injectJsonParser().toJson(contents);
        }

        StringBuilder sb = new StringBuilder();
        for (Object content : contents) {
            sb.append(content.toString()).append(";");
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }
}
